<?php

/**
 * corredor actions.
 *
 * @package    neoTrainner
 * @subpackage corredor
 * @author     Your name here
 */
class corredorActions extends sfActions {

    public function executePreRegistro(sfWebRequest $request) {
        $this->evento = NeoEventoPeer::retrieveByPk($request->getParameter('id'));
        $this->forward404Unless($this->evento, sprintf('No existe el evento.'));
        $this->form = new NeoPersonaForm();
    }

    public function executeNew(sfWebRequest $request) {

        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new NeoPersonaForm();
        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
        $idTipoDocumento = $this->form['tipo_documento_id']->getValue('tipo_documento_id');
        $numeroDocumento = $this->form['numero_documento']->getValue('numero_documento');

        $neoPersona = NeoPersonaPeer::findByTipoYNumeroDeDocumento($idTipoDocumento, $numeroDocumento);

        $this->evento = NeoEventoPeer::retrieveByPk($request->getParameter('id'));
        $this->forward404Unless($this->evento, sprintf('No existe el evento.'));

        $neoPersonaEvento = NeoPersonaEventoPeer::buscarPorPersonaYEvento($neoPersona, $this->evento);
        if ($neoPersonaEvento) {
            sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url'));
            $this->redirect(url_for('@consultaEstadoInscripcion')."/".$neoPersonaEvento->getId());
        }

        $this->form = new NeoPersonaForm($neoPersona);
        $this->form->setDefault('idEvento', $this->evento->getId());
        if ($neoPersona == null) {
            $this->form->setDefault('tipo_documento_id', $idTipoDocumento);
            $this->form->setDefault('numero_documento', $numeroDocumento);
        }
    }
    
    public function executeConsultaInscripcion(sfWebRequest $request) {
        $this->personaEvento = NeoPersonaEventoPeer::retrieveByPk($request->getParameter('idPersonaEvento'));
        $this->forward404Unless($this->personaEvento, sprintf('No existe la persona evento.'));
    }

    public function executeInscribir(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->form = new NeoPersonaEventoFrontendForm($request->getParameter('id'));
        $this->personaEvento = $this->processFormPersonaEvento($request, $this->form);
        if ($this->personaEvento) {
            $this->setTemplate('registroExitoso');
        } else {
            $this->setTemplate('registroErroneo');
        }
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $NeoPersona = NeoPersonaPeer::retrieveByPk($request->getParameter('id'));
        if ($NeoPersona) {
            $this->form = new NeoPersonaForm($NeoPersona);
        } else {
            $this->form = new NeoPersonaForm();
        }

        $idPersona = $this->processFormPersona($request, $this->form);

        $this->registrarPersona($idPersona, 'new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($NeoPersona = NeoPersonaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object NeoPersona does not exist (%s).', $request->getParameter('id')));
        $this->form = new NeoPersonaForm($NeoPersona);

        $this->evento = NeoEventoPeer::retrieveByPk($request->getParameter('id'));
        $this->forward404Unless($this->evento, sprintf('No existe el evento.'));
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($NeoPersona = NeoPersonaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object NeoPersona does not exist (%s).', $request->getParameter('id')));
        $this->form = new NeoPersonaForm($NeoPersona);

        $idPersona = $this->processFormPersona($request, $this->form);

        $this->registrarPersona($idPersona, 'edit');
    }

    public function executeRegistroExitoso(sfWebRequest $request) {
        
    }

    protected function processFormPersona(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $NeoPersona = $form->save();
            return $NeoPersona->getId();
        }
    }

    protected function processFormPersonaEvento(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $NeoPersonaEvento = $form->save();
            return $NeoPersonaEvento;
        }
    }

    private function registrarPersona($idPersona, $template) {
        $this->evento = NeoEventoPeer::retrieveByPk($this->form['idEvento']->getValue('idEvento'));
        $this->forward404Unless($this->evento, sprintf('No existe el evento.'));

        sfContext::getInstance()->getLogger()->debug("WWW Evento: " . $this->evento->getId());

        if ($idPersona) {
            $this->formPersonaEvento = new NeoPersonaEventoFrontendForm($this->evento->getId());
            $this->formPersonaEvento->setDefault('neo_persona_id', $idPersona);
            $this->formPersonaEvento->setDefault('neo_evento_id', $this->evento->getId());
            $this->formPersonaEvento->setDefault('estado', NeoPersonaEventoPeer::ESTADO_PRE_INSCRIPTO);
            $this->bases = NeoBasesYCondicionesPeer::doSelectOne(new Criteria());
            $this->setTemplate('registroEvento');
        } else {
            $this->setTemplate($template);
        }
    }

}
